//
//  CollectViewController.m
//  HandWork
//
//  Created by 王阳阳 on 16/2/26.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "CollectViewController.h"
#import "DetailSecondModel.h"
#import "DataBaseManager.h"
@interface CollectViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableview;//tableview
@property (nonatomic, strong)NSMutableArray *DarrArr;//tableview数据
@end

@implementation CollectViewController
-(void)dealloc
{
    [_tableview release];
    [_DarrArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //数据库
     DataBaseManager *dbManger = [DataBaseManager shareInstance];
    [dbManger openDB];
    //创建tableview
    self.tableview = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    //代理
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.view addSubview:self.tableview];
    //重用池
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
 //查询数据
    self.DarrArr = [[DataBaseManager shareInstance] selectMovie];
}

#pragma mark - tableview必须实现的的两个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _DarrArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text =  [_DarrArr[indexPath.row] subject];
    return cell;
}
//删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        DetailSecondModel *m = _DarrArr[indexPath.row];
        [[DataBaseManager shareInstance] deleteMovieWithName:m.subject];
        [_DarrArr removeObjectAtIndex:indexPath.row];
        [_tableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    
}
#pragma mark - tableview 点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
